package ch.fusun.baron.basic.turntask;

import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.core.rmi.User;
import ch.fusun.baron.core.service.UserService;
import ch.fusun.baron.player.Dynasty;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.property.api.PropertyService;
import ch.fusun.baron.treasury.api.TreasuryService;
import ch.fusun.baron.turn.TurnTask;
import ch.fusun.baron.unit.Unit;
import ch.fusun.baron.unit.service.UnitService;

/**
 * Computes the soldier expenses and subtracts them from the player treasury
 */
public class SoldierExpensesTurnTask implements TurnTask {

	@Inject
	private UserService userService;
	@Inject
	private PlayerService playerService;
	@Inject
	private TreasuryService treasuryService;
	@Inject
	private UnitService unitService;
	@Inject
	private PropertyService propertyService;

	/**
	 * Constructor
	 */
	public SoldierExpensesTurnTask() {
	}

	@Override
	public void execute() {
		for (User user : userService.getUsers()) {
			Dynasty dynasty = playerService.getDynasty(user);
			for (Unit unit : unitService.getAllUnits()) {
				if (dynasty.equals(propertyService.getOwnership(unit))) {
					treasuryService.removeMoney(dynasty,
							unit.getNumberOfUnits());
				}
			}
		}
	}
}
